package com.mikgonz.farkle;

import java.sql.SQLException;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.mikgonz.farkle.database.DatabaseOpenHelper;
import com.mikgonz.farkle.database.Friend;
import com.mikgonz.farkle.database.Message;
import com.mikgonz.farkle.database.Request;
import com.mikgonz.farkle.shared.FarkleRequestFactory;
import com.mikgonz.farkle.ui.ChatActivity;
import com.mikgonz.farkle.ui.OnlineActivity;

public class PushNotifReceiver extends BroadcastReceiver {

	public static final int NOTIF_ID = 3;
	private DatabaseOpenHelper helper;

	@Override
	public void onReceive(Context c, Intent og) {

		abortBroadcast();
		final String action = og.getStringExtra("NOTIFICATION_ACTION_NAME");
		if (action == null)
			return;

		helper = (DatabaseOpenHelper) OpenHelperManager.getHelper(c,
				DatabaseOpenHelper.class);
		
		if (action.equals(FarkleRequestFactory.NEW_MSG_ACTION)) {
			handleNewMessage(c, og);
		}

		if (action.equals(FarkleRequestFactory.NEW_FRIEND_REQUEST_ACTION)) {
			handleNewFriendRequest(c, og);
		}

		if (action.equals(FarkleRequestFactory.FRIEND_REQUEST_ACCEPTED_ACTION)) {
			handleAcceptance(c, og);
		}

		if (action.equals(FarkleRequestFactory.FRIEND_REQUEST_REJECTED_ACTION)) {
			handleRejection(c, og);
		}

		OpenHelperManager.releaseHelper();

	}

	private void handleAcceptance(Context c, Intent og) {

		String name = og.getStringExtra("NOTIFICATION_TITLE");
		String friendId = og.getStringExtra("NOTIFICATION_DETAILS");

		try {
			Dao<Request, String> dao = helper.getRequestDao();
			dao.delete(dao.queryForId(name));

			Dao<Friend, String> dao2 = helper.getFriendDao();
			Friend f = new Friend(name, Long.parseLong(friendId));
			dao2.create(f);

		} catch (SQLException e) {
			e.printStackTrace();
		}

		Intent acceptanceBroadcast = new Intent(c, OnlineActivity.class);

		PendingIntent contentIntent = PendingIntent.getActivity(c, 0,
				acceptanceBroadcast, 0);

		Util.createAndNotify(c, R.string.new_friend_ticker, contentIntent,
				R.string.new_friend_title, R.string.new_friend_details);

		acceptanceBroadcast = new Intent(
				FarkleRequestFactory.FRIEND_REQUEST_ACCEPTED_ACTION);

		c.sendBroadcast(acceptanceBroadcast);
	}

	private void handleRejection(Context c, Intent og) {

		String name = og.getStringExtra("NOTIFICATION_TITLE");

		try {
			Dao<Request, String> dao = helper.getRequestDao();
			dao.delete(dao.queryForId(name));
		} catch (SQLException e) {

			e.printStackTrace();
		}

		Intent rejectionBroadcast = new Intent(c, OnlineActivity.class);

		PendingIntent contentIntent = PendingIntent.getActivity(c, 0,
				rejectionBroadcast, 0);

		Util.createAndNotify(c, R.string.no_new_friend_ticker, contentIntent,
				R.string.no_new_friend_title, R.string.no_new_friend_details);

		rejectionBroadcast = new Intent(
				FarkleRequestFactory.FRIEND_REQUEST_REJECTED_ACTION);

		c.sendBroadcast(rejectionBroadcast);

	}

	private void handleNewFriendRequest(Context c, Intent og) {

		String name = og.getStringExtra("NOTIFICATION_TITLE");
		String id = og.getStringExtra("NOTIFICATION_DETAILS");

		Request incoming = new Request(name, Long.parseLong(id), 1);

		try {
			helper.getRequestDao().create(incoming);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Intent newRequest = new Intent(c, OnlineActivity.class);
		newRequest.putExtra(OnlineActivity.REQUEST_NAME, name);

		PendingIntent contentIntent = PendingIntent.getActivity(c, 0,
				newRequest, 0);

		Util.createAndNotify(c, R.string.new_request_ticker, contentIntent,
				R.string.new_request_title, R.string.new_request_details);

		newRequest = new Intent(FarkleRequestFactory.NEW_FRIEND_REQUEST_ACTION);
		newRequest.putExtra(OnlineActivity.FROM, name);

		c.sendBroadcast(newRequest);
	}

	private void handleNewMessage(Context c, Intent og) {
		String from = og.getStringExtra("NOTIFICATION_TITLE");
		String body = og.getStringExtra("NOTIFICATION_DETAILS");

		try {
			Message m = new Message(from, from, body);
			helper.getMessagesDao().create(m);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Intent newMSG = new Intent(c, ChatActivity.class);
		newMSG.putExtra(ChatActivity.FRIEND, from);

		PendingIntent contentIntent = PendingIntent
				.getActivity(c, 0, newMSG, 0);
		
		Util.createAndNotify(c, R.string.new_message_ticker, contentIntent,
				R.string.new_message_title, R.string.new_message_details);

		newMSG = new Intent(FarkleRequestFactory.NEW_MSG_ACTION);
		newMSG.putExtra(ChatActivity.FROM, from);
		newMSG.putExtra(ChatActivity.BODY, body);

		c.sendBroadcast(newMSG);
	}

}
